/**   
 * @Title: Project.java 
 * @Package org.playgod1984.du.ddesign.model 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author playgod playgod1984@163.com  
 * @date 2014年10月20日 下午5:18:53 
 * @version V1.0   
 */
package org.playgod1984.du.ddesign.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.playgod1984.du.auth.model.User;

import com.jfinal.log.Log4jLogger;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/** 
 * @ClassName: Project 
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @author playgod playgod1984@163.com 
 * @date 2014年10月20日 下午5:18:53 
 *  
 */
@SuppressWarnings("all")
public class Project extends Model<Project> {
	private static final Logger log = Log4jLogger.getLogger(Project.class);
	public static final Project dao = new Project();
	
	/**
	 * @Title: getUsersWithPagenate
	 * @author:playgod playgod1984@163.com 
	 * @Description: Query Users with pagenate 
	 * @param @param params
	 * @param @return    设定文件 
	 * @return Page<UserModel>    返回类型 
	 * @throws
	 */
	public Page<Project> getProjectsWithPagenate(Map params){
		int pageNumber = NumberUtils.toInt((String)params.get("page"));
		int pageSize = NumberUtils.toInt((String)params.get("pagesize"));
		String sql_where=" where 1=1 ";
		if(StringUtils.isNotBlank((String)params.get("xm"))){
			sql_where+=" and displayname like '%"+params.get("xm")+"%'";
			sql_where+=" or loginaccount like '%"+params.get("xm")+"%'";
		}
		String sqlselect="SELECT * ";
		return paginate(pageNumber, pageSize, sqlselect, "FROM dud_ddesign_pro pro" + sql_where);
	}
	/**
	 * 
	 * @Title: deleteRows
	 * @author:playgod playgod1984@163.com 
	 * @Description: TODO(这里用一句话描述这个方法的作用) 
	 * @param @param params
	 * @param @return    设定文件 
	 * @return int    返回类型 
	 * @throws
	 */
	public int deleteRows(Map params){
		int results =0;
		try{
			String[] ids = StringUtils.split((String)params.get("keys"),",");
			for(int i=0;i<ids.length;i++){
				if(dao.deleteById(ids[i])){
					results++;
				}
			}
			return results;
		}catch(Exception ex){
			log.error(ex.getMessage());
			return -1;
		}
	}
	/**
	 * @Title: findByName
	 * @author:playgod playgod1984@163.com 
	 * @Description: select by name 
	 * @param @param projectname
	 * @param @return    设定文件 
	 * @return Project    返回类型 
	 * @throws
	 */
	public Project findByName(String projectname){
		String sql="select projectcode from "
				+ "	dud_ddesign_pro where projectname='"+projectname+"'";
		return findFirst(sql);		
	}
	/**
	 * @Title: findByNameWithDiffCode
	 * @author:playgod playgod1984@163.com 
	 * @Description: Same check for edit
	 * @param @return    设定文件 
	 * @return List<Project>    返回类型 
	 * @throws
	 */
	public Project findByNameWithDiffCode(Map parmas){
		String sql = "select projectcode from "
				+ "	dud_ddesign_pro where projectname='"+parmas.get("projectname")+"'"
				+" and projectcode != '" + parmas.get("projectcode")+"'";
		return findFirst(sql);
	}
	/**
	 * @Title: save
	 * @author:playgod playgod1984@163.com 
	 * @Description: save
	 * @param @param params
	 * @param @return    设定文件 
	 * @return boolean    返回类型 
	 * @throws
	 */
	public boolean save(Map params){
		Record r = new Record();
		r.set("projectcode", params.get("projectcode"));
		r.set("projectname", params.get("projectname"));
		r.set("remark", params.get("remark"));
		r.set("updator", params.get("currentUser"));
		r.set("updatetime",new Date());
		try{
			if("A".equals((String)params.get("screen"))){//ADD
				r.set("creator", params.get("currentUser"));
				r.set("createtime",new Date());
				Db.save("dud_ddesign_pro", r);
			}else{
				Db.update("dud_ddesign_pro", "projectcode", r);
			}
			return true;
		}catch(Exception ex){
			log.error(ex.getMessage());
			return false;
		}
	}
	/**
	 * @Title: userWithProject
	 * @author:playgod playgod1984@163.com 
	 * @Description: TODO(这里用一句话描述这个方法的作用) 
	 * @param @param projectcode
	 * @param @return    设定文件 
	 * @return Page<User>    返回类型 
	 * @throws
	 */
	public Page<User> getUserWithProject(Map params){
		int pageNumber = NumberUtils.toInt((String)params.get("page"));
		int pageSize = NumberUtils.toInt((String)params.get("pagesize"));
		String sqlselect="SELECT  u.loginaccount,u.displayname";
		String sql_where=" where pu.projectcode = '"+params.get("projectcode")+"'";
		if(StringUtils.isNotEmpty((String)params.get("displayname"))){
			sql_where+=" and u.displayname like'%"+params.get("displayname")+"%'";
		}
		return User.dao.paginate(pageNumber, pageSize, sqlselect, "FROM dud_auth_user u"
				+ " INNER JOIN dud_ddesign_pu pu ON u.loginaccount = pu.loginaccount"
				+ sql_where);
	}
	/**
	 * @Title: getUserWithoutProject
	 * @author:playgod playgod1984@163.com 
	 * @Description: TODO(这里用一句话描述这个方法的作用) 
	 * @param @param params
	 * @param @return    设定文件 
	 * @return Page<User>    返回类型 
	 * @throws
	 */
	public Page<User> getUserWithoutProject(Map params){
		int pageNumber = NumberUtils.toInt((String)params.get("page"));
		int pageSize = NumberUtils.toInt((String)params.get("pagesize"));
		String sqlselect="SELECT  u.loginaccount,u.displayname";
		String sql_where=" where pu.projectcode IS NULL";
		if(StringUtils.isNotEmpty((String)params.get("displayname"))){
			sql_where+=" and u.displayname like'%"+params.get("displayname")+"%'";
		}
		return User.dao.paginate(pageNumber, pageSize, sqlselect, "FROM dud_auth_user u"
				+ " LEFT JOIN dud_ddesign_pu pu ON u.loginaccount = pu.loginaccount"
				+ " AND pu.projectcode = '"+params.get("projectcode")+"'"
				+ sql_where);
	}
	/**
	 * @Title: addUser
	 * @author:playgod playgod1984@163.com 
	 * @Description: Add User
	 * @param @return    设定文件 
	 * @return boolean    返回类型 
	 * @throws
	 */
	public boolean addMumber(Map params){
		Record r = new Record();
		r.set("projectcode", params.get("projectcode"));
		r.set("loginaccount", params.get("loginaccount"));
		r.set("updator", params.get("currentUser"));
		r.set("updatetime",new Date());
		r.set("creator", params.get("currentUser"));
		r.set("createtime",new Date());
		return Db.save("dud_ddesign_pu", r);
	}
	/**
	 * @Title: removeUser
	 * @author:playgod playgod1984@163.com 
	 * @Description: remove user 
	 * @param @return    设定文件 
	 * @return int    返回类型 
	 * @throws
	 */
	public boolean removeMumber(Map params){
		Record r = new Record();
		r.set("projectcode", params.get("projectcode"));
		r.set("loginaccount", params.get("loginaccount"));
		return Db.update("delete from dud_ddesign_pu  where projectcode='"+params.get("projectcode") +"'"+
		" and loginaccount='"+params.get("loginaccount") +"'") >= 1;
	}
}
